package models;

import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.TableGenerator;

import play.data.validation.Constraints.Required;
import play.db.ebean.Model;

@Entity
@Table(name = "schedulejob")
public class ScheduleJob extends Model {

	private static final long serialVersionUID = 1L;

	@Column(name = "id")
	@GeneratedValue(generator = "SEQ_TABLE", strategy = GenerationType.TABLE)
	@Id
	@TableGenerator(name = "SEQ_TABLE")
	private long id;

	@Column(name = "company", nullable = false)
	@ManyToOne(fetch = FetchType.LAZY)
	private Company company;

	@Column(name = "name", nullable = false)
	@Required
	private String name;

	@Column(name = "description")
	private String description;

	@Column(name = "test_project")
	@JoinTable(name = "projects")
	@ManyToOne(fetch = FetchType.LAZY)
	private Project testProject;

	@Column(name = "test_config")
	@JoinTable(name = "resources")
	@ManyToOne(fetch = FetchType.LAZY)
	private Resource testConfig;

	@Column(name = "created")
	private Date created;

	@Column(name = "last_modified")
	private Date lastModified;

	@Column(name = "schedule")
	private String schedule;
	
	@Column(name = "notification_emails")
	private String notificationEmails;
	
	public static final Finder<Long, ScheduleJob> find = new Finder<Long, ScheduleJob>(Long.class, ScheduleJob.class);

	public ScheduleJob(Company company, String name) {
		this.company = company;
		this.name = name;
	}

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public Company getCompany() {
		return company;
	}

	public void setCompany(Company company) {
		this.company = company;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public Date getCreatedDate() {
		return created;
	}

	public void setCreatedDate(Date created) {
		this.created = created;
	}

	public Date getLastModifiedDate() {
		return lastModified;
	}

	public void setLastModifiedDate(Date lastModified) {
		this.lastModified = lastModified;
	}

	public static ScheduleJob findById(long scheduleJobId) {
		return find.byId(scheduleJobId);
	}

	public static List<ScheduleJob> findScheduleJobsByCompany(Company company) {
		return find.where().eq("company.id", company.getId()).findList();
	}

	public String getSchedule()
	{
		return schedule;
	}

	public void setSchedule(String schedule)
	{
		this.schedule = schedule;
	}

	public String getNotificationEmails()
	{
		return notificationEmails;
	}

	public void setNotificationEmails(String notificationEmails)
	{
		this.notificationEmails = notificationEmails;
	}

	public Project getTestProject()
	{
		return testProject;
	}

	public void setTestProject(Project testProject)
	{
		this.testProject = testProject;
	}

	public Resource getTestConfig()
	{
		return testConfig;
	}

	public void setTestConfig(Resource testConfig)
	{
		this.testConfig = testConfig;
	}

	public Date getLastModified()
	{
		return lastModified;
	}

	public void setLastModified(Date lastModified)
	{
		this.lastModified = lastModified;
	}

	public Date getCreated()
	{
		return created;
	}

	public void setCreated(Date created)
	{
		this.created = created;
	}

	@Override
	public String toString()
	{
		return "ScheduleJob [id=" + id + ", company=" + company + ", name="
				+ name + ", description=" + description + ", testProject="
				+ testProject + ", testConfig=" + testConfig + ", created="
				+ created + ", lastModified=" + lastModified + ", schedule="
				+ schedule + ", notificationEmails=" + notificationEmails + "]";
	}

	

	

}
